<?php

namespace app\common\service;


use app\common\model\ArticleModel;

class ArticleService
{

    public function _info($id)
    {
        $db   = new ArticleModel();
        $info = $db->where('id', $id)->find();
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $info]);
    }

    public function _list($page, $limit)
    {
        $db    = new ArticleModel();
        $list  = $db->page($page)->limit($limit)->select();
        $count = $db->count('id');
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
    }

    public function _add($data)
    {
        $db  = new ArticleModel();
        $res = $db->save($data);
        if ($res) {
            return json(['code' => 1, 'msg' => '操作成功']);
        } else {
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    public function _edit($data)
    {
        $db  = new ArticleModel();
        $res = $db->save($data, ['id' => $data['id']]);
        if ($res) {
            return json(['code' => 1, 'msg' => '操作成功']);
        } else {
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }

    public function _delete($id)
    {
        $db  = new ArticleModel();
        $res = $db->where('id', $id)->delete();
        if ($res) {
            return json(['code' => 1, 'msg' => '操作成功']);
        } else {
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }
}
